package com.study.spmongodb.wwj.thread2.char10;

import java.util.HashMap;
import java.util.Map;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/8 20:56
 * 始终以当前thread作为key。
 */
public class ThreadLocalSimulator<T> {
    private final Map<Thread, T> storge = new HashMap<>();

    public void set(T t) {
        synchronized (this) {
            final Thread thread = Thread.currentThread();
            storge.put(thread, t);
        }
    }

    public T get() {
        synchronized (this) {
            final T t = storge.get(Thread.currentThread());
            if (null == t)
                return initValue();
            return t;
        }
    }

    public T initValue() {
        return null;
    }
}
